package com.simdo.g768.Dialog;

import static com.simdo.g768.MainActivity.device;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.g768.MessageControl.MessageController;
import com.g768.Util.IpUtil;
import com.simdo.g768.MainActivity;
import com.simdo.g768.Mqtt.MqttBean;
import com.simdo.g768.R;
import com.simdo.g768.Util.GsonUtil;
import com.simdo.g768.Util.PrefUtil;

public class MqttDialog extends Dialog {

    private final Context mContext;

    public MqttDialog(Context context) {
        super(context, R.style.Theme_B97621);
        this.mContext = context;

        View view = View.inflate(context, R.layout.dialog_mqtt, null);
        this.setContentView(view);
        this.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //this.setCancelable(false);   // 返回键不消失
        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window window = this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.setNavigationBarColor(Color.parseColor("#2A72FF"));
        //window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        //StatusBarUtil.setLightStatusBar(window, true);
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);

        initView(view);
    }

    private void initView(View root) {
        root.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        MqttBean mqttBean = MainActivity.getInstance().mqttConnection.getMqttBean();

        EditText ed_server_ip = root.findViewById(R.id.ed_server_ip);
        ed_server_ip.setHint(MainActivity.getInstance().fromHtml("Server ip"));
        ed_server_ip.setText(mqttBean.getHost());

        EditText ed_client_id = root.findViewById(R.id.ed_client_id);
        ed_client_id.setText(mqttBean.getPub_client());

        EditText ed_pub_topic = root.findViewById(R.id.ed_pub_topic);
        ed_pub_topic.setText(mqttBean.getPub_topic());

        EditText ed_pub_user = root.findViewById(R.id.ed_pub_user);
        ed_pub_user.setText(mqttBean.getPub_user());

        EditText ed_pub_passwd = root.findViewById(R.id.ed_pub_passwd);
        ed_pub_passwd.setText(mqttBean.getPub_passwd());

        EditText ed_user = root.findViewById(R.id.ed_user);
        ed_user.setText(mqttBean.getUsername());

        EditText ed_passwd = root.findViewById(R.id.ed_passwd);
        ed_passwd.setText(mqttBean.getPassword());

        Switch switch_mqtt_dev = root.findViewById(R.id.switch_mqtt_dev);
        switch_mqtt_dev.setChecked(mqttBean.getPub_enable() == 1);
        switch_mqtt_dev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = switch_mqtt_dev.isChecked();
                if (device == null){
                    MainActivity.getInstance().showToast(mContext.getString(R.string.dev_offline));
                    switch_mqtt_dev.setChecked(!checked);
                    return;
                }

                mqttBean.setPub_enable(checked ? 1 : 0);
                String server_ip = ed_server_ip.getText().toString();
                String client_id = ed_client_id.getText().toString();
                String pub_topic = ed_pub_topic.getText().toString();
                String pub_user = ed_pub_user.getText().toString();
                String pub_passwd = ed_pub_passwd.getText().toString();
                if (checked){
                    if (!IpUtil.build().checkIP(server_ip)){
                        MainActivity.getInstance().showToast(mContext.getString(R.string.ip_err));
                        switch_mqtt_dev.setChecked(false);
                        return;
                    }

                    if (pub_topic.isEmpty()){
                        MainActivity.getInstance().showToast(mContext.getString(R.string.pub_topic_empty));
                        switch_mqtt_dev.setChecked(false);
                        return;
                    }
                }
                mqttBean.setHost(server_ip);
                mqttBean.setPub_client(client_id);
                mqttBean.setPub_topic(pub_topic);
                mqttBean.setPub_user(pub_user);
                mqttBean.setPub_passwd(pub_passwd);
                MainActivity.getInstance().showToast(mContext.getString(R.string.cfging));
                MessageController.build().setMqttCfg(device.getId(), mqttBean.getHost(), mqttBean.getPub_client(), mqttBean.getPub_topic(), mqttBean.getPub_user(), mqttBean.getPub_passwd(), mqttBean.getPub_enable());
                PrefUtil.build().putString("mqtt_info", GsonUtil.createJsonString(mqttBean));
            }
        });

        TextView tv_mqtt_app_save = root.findViewById(R.id.tv_mqtt_app_save);
        tv_mqtt_app_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mqttBean.getHost().isEmpty()){
                    String server_ip = ed_server_ip.getText().toString();
                    if (!IpUtil.build().checkIP(server_ip)){
                        MainActivity.getInstance().showToast(mContext.getString(R.string.ip_err));
                        return;
                    }
                    mqttBean.setHost(server_ip);
                }

                String user = ed_user.getText().toString();
                String passwd = ed_passwd.getText().toString();

                mqttBean.setUsername(user);
                mqttBean.setPassword(passwd);
                PrefUtil.build().putString("mqtt_info", GsonUtil.createJsonString(mqttBean));
                MainActivity.getInstance().showToast("保存成功，正在配置MQTT接收");
                MainActivity.getInstance().openMqtt();
            }
        });

    }
}
